package com.example.tableviewdemo;

import com.example.tableviewdemo.Entity.Person;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.util.Callback;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

public class TableTestController implements Initializable {
    private static final int PAGE_SIZE = 5;
    @FXML
    private TableColumn<Person, Boolean> check;
    @FXML
    private TableColumn<Person, String> username;
    @FXML
    private TableColumn<Person, Integer> age;

    @FXML
    private TableColumn<Person, String> job;

    @FXML
    private Pagination pagination;

    @FXML
    private TableView<Person> table;

    @FXML
    private VBox vbox;
    @FXML
    protected void onHelloButtonClick() {
        System.out.println("table 测试！");
    }


    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {

        check.setCellValueFactory(new PropertyValueFactory("check"));
        username.setCellValueFactory(new PropertyValueFactory("name"));
        age.setCellValueFactory(new PropertyValueFactory("age"));
        job.setCellValueFactory(new PropertyValueFactory("job"));
        List<Person> data = getData();

        table.setMinHeight(328);
        check.setCellFactory(new Callback<TableColumn<Person, Boolean>, TableCell<Person, Boolean>>() {
            @Override
            public TableCell<Person, Boolean> call(TableColumn<Person, Boolean> personStringTableColumn) {
                return new TableCell<Person, Boolean>() {
                    private CheckBox checkBox = new CheckBox();

                    @Override
                    public void updateItem(Boolean item, boolean empty) {
                        super.updateItem(item, empty);
                        if (empty) {
                            setGraphic(null);
                        } else {
                            if(item != null) {
                                checkBox.setSelected(item);
                            }
                            setGraphic(checkBox);
                            // 监听CheckBox的选择事件
                            checkBox.selectedProperty().addListener((obs, wasSelected, isNowSelected) -> {
                                // 更新表格中的数据
                                Person row = getTableView().getItems().get(getIndex());
                                row.setCheck(isNowSelected);

                            });
                        }
                    }
                };

            }
        });

        //分页
        //计算总页数
        int pageCount = data.size() % 10 == 0?data.size() / 10:data.size() / 10+1;
        //pagination.getStyleClass().add(Pagination.STYLE_CLASS_BULLET);
        pagination.setPageCount(pageCount);
        pagination.setCurrentPageIndex(0);
        pagination.setPageFactory(pageIndex -> {
            return createPage(pageIndex);
        });

    }

    public List<Person> getData(){
        List<Person> data = new ArrayList<>();
        ClassLoader classLoader = TableTestController.class.getClassLoader();

        try (InputStream inputStream = classLoader.getResourceAsStream("data.txt"); BufferedReader br = new BufferedReader(new InputStreamReader(inputStream))) {
            String line;
            while ((line = br.readLine()) != null) {
                Person person = new Person();
                String[] split = line.split(",");
                boolean checkVal = split[0].equals("true")?true:false;

                person.setCheck(checkVal);
                person.setName(split[1]);
                person.setAge(Integer.parseInt(split[2]));
                person.setJob(split[3]);

                data.add(person);

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return data;
    }


    private VBox createPage(int pageIndex) {

        System.out.println("pageIndex: "+pageIndex);
        List<Person> data = getData();
        List<Person> subList;
        int end = pageIndex * 10 + 10;
        if(end > data.size()){
            end = data.size();
        }

        subList = data.subList(pageIndex * 10, end);
        System.out.println("======================================");
        for (Person p:subList) {
            System.out.println(p.getName()+"    "+p.getName()+"    "+p.getAge()+"   "+p.getJob());
        }

        ObservableList<Person> items = FXCollections.observableArrayList(subList);
        table.setItems(items);

        return new VBox(table);


    }


}